package com.common.core.cookie;

import com.common.utils.CookieUtil;
import com.common.utils.JwtUtils;
import com.common.utils.UUIDUtil;
import com.vo.UserVO;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yz
 * @data: 2022/4/7 20:11 星期四
 * @file : GenerateCookie.java
 */
public class GenerateCookie {

    /**
     * 存储用户信息到redis
     * cookie写入浏览器
     *
     * @param request
     * @param response
     * @param redisTemplate
     * @param userVO
     */
    public static void generateCookie(HttpServletRequest request, HttpServletResponse response, RedisTemplate<String, Object> redisTemplate, UserVO userVO) {
        //生成cookie
        String ticTicker = UUIDUtil.uuid();
        String token = JwtUtils.generateToken(userVO.getLoginName());
        userVO.setPassword("想看我密码想得美");
        userVO.setSalt("");
//        redisTemplate.opsForValue().set("user:" + ticTicker, userVO);

        redisTemplate.opsForValue().set("user:" + userVO.getLoginName(), token);
        redisTemplate.opsForValue().set("Token:" + token, userVO);
//        CookieUtil.setCookie(request, response, "userTicket", ticTicker);
        CookieUtil.setCookie(request, response, "userToken", token);
    }
}
